<?php

class P2C_Batch {

	var $initial = false;

	var $id;
	var $start_date;
	var $end_date;
    var $status_id;
    var $invoked_by;
    var $platforms;

	public static function find( $args = '' ) {
        global $wpdb;
        
		$defaults = array(
			'post_status' => 'any',
			'posts_per_page' => -1,
			'offset' => 0,
			'orderby' => 'id',
			'order' => 'ASC' );

		$args = wp_parse_args( $args, $defaults );

		$batches = $wpdb->get_results("SELECT id, start_date, end_date, status_id, invoked_by
            FROM p2c_batch
            ORDER BY " . $args['orderby'] . " " . $args['order'] . " 
            LIMIT " . $args['offset'] . "," . $args['posts_per_page'] . ";");

		$objs = array();

		foreach ( $batches as $batch )
			$objs[] = new self( $batch );

		return $objs;
	}
    
    public static function rowcount( ) {
        global $wpdb;

		$batches = $wpdb->get_var("SELECT COUNT(*) FROM p2c_batch;");
        
		return $batches;
	}

	public function __construct( $batch = null ) {
        global $wpdb, $current_user;
        get_currentuserinfo();
        
		$this->initial = true;
        $this->start_date = date('Y-m-d H:i:s');
        $this->status_id = 0;
        $this->invoked_by = $current_user->user_login;
        
        $this->platforms = array();
                
        if($batch && !is_object($batch)) {
            $batch = $wpdb->get_row("SELECT id, start_date, end_date, status_id, invoked_by FROM p2c_batch WHERE id = " . (int) $batch);
        }
        
		if ( $batch ) {
			$this->initial = false;
			$this->id = $batch->id;
			$this->start_date = $batch->start_date;
			$this->end_date = $batch->end_date;
			$this->status_id = $batch->status_id;
            $this->invoked_by = $batch->invoked_by;
		}
    }
    
    public function finish() {
            $this->end_date = date('Y-m-d H:i:s');
            $this->status_id = 1;
            $this->save();   
    }
    
    public function error() {
            $this->end_date = date('Y-m-d H:i:s');
            $this->status_id = 2;
            $this->save();   
    }
    
    public function addPlatform($platform) {
        global $wpdb; 
        
        if( !$this->initial && !$platform->initial ) {
            $wpdb->insert("p2c_batch_platform", array(
               "batch_id" => $this->id,
                "platform_id" => $platform->id
            ));
            
            array_push($this->platforms, $platform);
        }
    }
    
    public function getPlatforms() {
        global $wpdb; 
        
        $this->platforms = array();
        if (!$this->initial) {
            $pfs = $wpdb->get_results("SELECT platform_id FROM p2c_batch_platform WHERE batch_id = " . (int) $this->id);
            
            foreach( $pfs as $pf ) {
                $platform = new P2C_Platform($pf->platform_id);
                if(!$platform->inital) {
                    array_push($platforms, $platform);
                }
            }
        }
        
        return $platforms;
    }
    
	/* Save */

	function save() {
        global $wpdb; 
        
		if ( $this->initial ) {
			$wpdb->insert("p2c_batch", array(
               "start_date" => $this->start_date,
               "end_date" => $this->end_date,
               "status_id" => $this->status_id,
               "invoked_by" => $this->invoked_by
            ));
            
            $this->initial = false;
            $this->id = $wpdb->insert_id;
		} else {
			$wpdb->update( 
                'p2c_batch', 
                array( 
                   "end_date" => $this->end_date,
                   "status_id" => $this->status_id
                ), 
                array( 'id' => $this->id )
            );
		}

		return $this->id;
	}
    
    function parse() {
        
    }

}


?>